<?php

/**
 * Created by PhpStorm.
 * User: frank
 * Date: 2021-07-20
 * Time: 9:15
 */

namespace App\Helpers;


use App\Models\Building;
use App\Models\Dorm;

class Utils
{
    static function gen_dorms($building_id)
    {
        Dorm::query()->where('building_id', $building_id)->delete();
        $building = Building::find($building_id);

        $letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
        $n = 0;
        $rule = $building->gen_dorm_rule;
        if ($rule) {
            $name = $building->name;
            $short = $building->short_name;
            for ($i = 0; $i < $building->floors; $i++) {
                $floor = $i + 1;
                for ($j = 0; $j < $building->unit_count; $j++) {
                    $unit = $building->unit_name_type == 'number' ? $j + 1 : $letters[$j];
                    for ($k = 0; $k < $building->rooms_per_floor; $k++) {
                        $sn = $k + 1;
                        $code = self::make_code_by_rule($name, $rule, $short, $floor, $unit, $sn);

                        Dorm::create([
                            'company_id' => $building->company_id ?? auth()->user()->company_id,
                            'code' => $code,
                            'building_id' => $building->id,
                            'dorm_type_id' => $building->dorm_type_id,
                            'for_gender' => $building->dormType->for_gender,
                            'beds' => $building->beds_per_room,
                            'unit_name' => $unit,
                            'floor' => $floor,
                        ]);
                        $n++;
                    }
                }
            }
        }
        return $n;
    }

    static function get_menus()
    {
        return collect(
            [
                '人员管理' => ['staff_list' => '人员列表', 'staff_dorm_record' => '宿管记录'],
                '宿舍管理' => ['dorm_list' => '宿舍列表', 'dorm_meter_record' => '抄表记录', 'dorm_meter_manage' => '表管理记录'],
                '宿舍评比' => ['rating-categories.index' => '评分大类', 'rating-subitems.index' => '评分项', 'dormitory-ratings.index' => '评分明细', 'dorm-rating-mains.index' => '评分主表'],
                '费用管理' => ['fee_period' => '费用周期', 'fee_list' => '费用列表', 'deposit_list' => '押金列表'],
                '用户管理' => ['user_list' => '用户列表', 'role_list' => '角色列表', 'permssion_type' => '权限类别', 'permission_list' => '权限列表'],
                '基础数据' => ['companies.index' => '公司信息', 'dorm-types.index' => '计量表类型', 'staff_data' => '人事数据', 'dorm_data' => '宿舍数据', 'fee_data' => '费用数据'],
            ]
        );
    }

    /**
     * 可选的计算费用的方法有
     * --简单按用量来计算费用
     * calcSimpleTypeConsumeMoney
     * --按人头来计算费用，没有用量，比如管理费
     * calcSimpleTypeMoneyWithoutConsume
     * --按某个梯度价格乘以用量来计算费用
     * calcTypesConsumeMoneyByMeetOne
     * --用量分梯度并乘以梯度价格最后累加
     * calcTypesConsumeMoneyByMeetLevels
     * @return \Illuminate\Support\Collection
     */
    static function get_fee_types()
    {

        return collect(
            [
                (object)['id' => 'water_fee_type_id', 'type_name' => 'waterFeeType', 'model' => 'App\Models\WaterFeeType', 'name' => 'water_fee', 'cn_name' => '水费', 'meter_type_id' => 1, 'is_simple' => true, 'enable' => true, 'calc_method' => 'calcSimpleTypeConsumeMoney'],
                (object)['id' => 'hot_water_fee_type_id', 'type_name' => 'hotWaterFeeType', 'model' => 'App\Models\HotWaterFeeType', 'name' => 'hot_water_fee', 'cn_name' => '热水费', 'meter_type_id' => 3, 'is_simple' => true, 'enable' => true, 'calc_method' => 'calcSimpleTypeConsumeMoney'],
                (object)['id' => 'electric_fee_type_id', 'type_name' => 'electricFeeType', 'model' => 'App\Models\ElectricFeeType', 'name' => 'electric_fee', 'cn_name' => '电费', 'meter_type_id' => 2, 'is_simple' => false, 'enable' => true, 'calc_method' => 'calcTypesConsumeMoneyByMeetOne'],
                (object)['id' => 'manage_fee_type_id', 'type_name' => 'manageFeeType', 'model' => 'App\Models\ManageFeeType', 'name' => 'manage_fee', 'cn_name' => '管理费', 'meter_type_id' => null, 'is_simple' => true, 'enable' => true, 'calc_method' => 'calcSimpleTypeMoneyWithoutConsume'],
            ]
        );
    }

    static function get_dorm_type_auto_fix()
    {
        return collect([
            ['work_years' => 2, 'floor_lower' => 1, 'from_type_id' => 9, 'to_type_id' => 11],
            ['work_years' => 5, 'floor_lower' => 1, 'from_type_id' => 11, 'to_type_id' => 13],
            ['work_years' => 2, 'floor_lower' => 2, 'from_type_id' => 2, 'to_type_id' => 10],
            ['work_years' => 5, 'floor_lower' => 2, 'from_type_id' => 10, 'to_type_id' => 12],
        ]);
    }

    /**
     * @param $name
     * @param $rule
     * @param $short
     * @param int $floor
     * @param int $unit
     * @param int $sn
     * @param $ms
     * @return mixed|string|string[]|null
     */
    public static function make_code_by_rule($name, $rule, $short, int $floor, int $unit, int $sn)
    {
        $code = str_replace('{楼栋}', $name, $rule);
        $code = str_replace('{简称}', $short, $code);
        $code = str_replace('{楼层}', $floor, $code);
        $code = str_replace('{单元}', $unit, $code);
        $pattern = '/\{流水号:(0+)\}/';
        if (preg_match($pattern, $rule, $ms)) {
            $sn = str_pad($sn, strlen($ms[1]), '0', STR_PAD_LEFT);
            $code = preg_replace($pattern, $sn, $code);
        }
        return $code;
    }
}
